package admin_user_api

import (
	"blog_go/global"
	"blog_go/models"
	"blog_go/models/ctype"
	"blog_go/models/res"
	"github.com/fatih/structs"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type UserUpdateReq struct {
	UserID   uint       `json:"id" binding:"required" structs:"-"`
	Role     ctype.Role `json:"role" binding:"required,oneof=1 2 3 4" structs:"role"` // oneof 只能填写这些值
	NickName string     `json:"nick_name" binding:"required" structs:"nick_name"`
}

func (AdminUserApi) UserUpdate(c *gin.Context) {
	var req UserUpdateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		logrus.Error(err)
		panic(err)
	}
	// 查询数据是否存在
	var userModel models.UserModel
	err := global.DB.Take(&userModel, req.UserID).Error
	if err != nil {
		panic("数据不存在")
	}
	// 去修改数据
	updateMap := structs.Map(req)
	err = global.DB.Model(&userModel).Updates(updateMap).Error
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	res.Ok("修改成功", true, c)
}
